package com.bolt.support.captcha.impl;

import com.bolt.common.utils.SpringContextUtil;
import com.bolt.support.captcha.CaptchaCacheService;
import com.bolt.support.captcha.CaptchaTypeEnum;
import com.bolt.support.captcha.CaptchaProperties;
import com.bolt.support.captcha.CaptchaService;

public class CaptchaServiceFactory {

    public static synchronized CaptchaCacheService getCache() {
        CaptchaCacheService captchaCacheService = SpringContextUtil.getBean(CaptchaCacheService.class);
        if (null == captchaCacheService) {
            captchaCacheService = (LocalCaptchaService) SpringContextUtil.createInjectBean(LocalCaptchaService.class);
        }
        return captchaCacheService;
    }

    public static synchronized CaptchaService getCaptchaService(CaptchaProperties config) {
        CaptchaTypeEnum captchaEnum = config.getCodeType();
        if (CaptchaTypeEnum.blockPuzzle == captchaEnum) {
            return new BlockPuzzleCaptchaServiceImpl(config);
        } else {
            return new EasyCaptchaServiceImpl(config);
        }
    }
}